//
//  SelfHeaderView2.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/4/2.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "SelfHeaderItemView.h"
#import "SelfHeaderBtn.h"
#define HeaderItemInsert 38
#define RED_POINT_WIDTH 16

@interface SelfHeaderItemView()
{
    UILabel *_badgeLabel;
}
@end

@implementation SelfHeaderItemView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self!=nil){
        
        //订单,红点
        SelfHeaderBtn *order = [[SelfHeaderBtn alloc]init];
        UIImage *orderImg = [UIImage imageNamed:@"icon_self_order_normal"];
        CGRect orderRect = CGRectMake(HeaderItemInsert, JXPublicInset, orderImg.size.width + JXPublicInset*2, [DimensUtils getScaleX:orderImg.size.height]);
        [self initButton:order img:orderImg title:@"订单" frame:orderRect target:@selector(clickItemOrder)];
        
        //红点
        _badgeLabel = [[UILabel alloc]init];
        _badgeLabel.x = CGRectGetMaxX(order.frame)-JXPublicInset;
        _badgeLabel.y = 0;
        [_badgeLabel setBackgroundColor:[UIColor redColor]];
        [_badgeLabel setTextColor:[UIColor whiteColor]];
        [_badgeLabel setTextAlignment:NSTextAlignmentCenter];
        [_badgeLabel setFont:[UIFont systemFontOfSize:10]];
        [_badgeLabel setSize:CGSizeMake(RED_POINT_WIDTH, RED_POINT_WIDTH)];
        [_badgeLabel.layer setCornerRadius:RED_POINT_WIDTH/2];
        [_badgeLabel.layer setMasksToBounds:YES];
        [self addSubview:_badgeLabel];
        
        //代金卷
        SelfHeaderBtn *volume = [[SelfHeaderBtn alloc]init];
        UIImage *volumeImg = [UIImage imageNamed:@"icon_self_volume_normal"];
        CGRect volumeRect = CGRectMake(JXScreenW/2-(volumeImg.size.width +JXPublicInset*2)/2, JXPublicInset, volumeImg.size.width + JXPublicInset*2, volumeImg.size.height);
        [self initButton:volume img:volumeImg title:@"现金券" frame:volumeRect target:@selector(clickItemVolume)];
       
        //收藏
        SelfHeaderBtn *mark = [[SelfHeaderBtn alloc]init];
        UIImage *markImg = [UIImage imageNamed:@"icon_self_mark_normal"];
        CGRect markRect = CGRectMake(JXScreenW - ((markImg.size.width + JXPublicInset*2) + HeaderItemInsert), JXPublicInset, markImg.size.width + JXPublicInset*2, markImg.size.height);
        [self initButton:mark img:markImg title:@"收藏" frame:markRect target:@selector(clickItemMark)];
        self.frame = CGRectMake(0, 0, JXScreenW, CGRectGetMaxY(mark.frame));
    }
    return self;
}

/**
 *
 *  @param btn    btn
 *  @param img    图片
 *  @param title  描述
 *  @param frame  frame
 *  @param target target
 */
-(void) initButton:(SelfHeaderBtn *)btn img:(UIImage *)img title:(NSString *)title frame:(CGRect)frame target:(SEL)target{
    btn.backgroundColor = [UIColor clearColor];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:img forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:BUTTON_NAME_SIZE]];
    [btn setTitleColor:JXColor(102, 102, 102) forState:UIControlStateNormal];
    btn.frame = frame;
    [btn addTarget:self action:target forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}

/**
 *  点击订单
 */
-(void) clickItemOrder
{
   [_delegate clickItemOrder];
}

/**
 *  点击收藏
 */
-(void) clickItemMark
{
    [_delegate clickItemMark];
}

/**
 *  点击代金卷
 */
-(void) clickItemVolume
{
   [_delegate clickItemVolume];
}
/**
 *  设置订单数
 *
 *  @param num 订单数
 */
-(void)setBadgeNum:(NSInteger)num{
    if (num==0) {
        [_badgeLabel setHidden:YES];
    }else{
        [_badgeLabel setText:[NSString stringWithFormat:@"%ld",(long)num]];
        [_badgeLabel setHidden:NO];
        //调整位置
//        [_badgeLabel setX:CGRectGetMaxX(_titleLabel.frame)+5];
//        [_badgeLabel setY:CGRectGetMinY(_titleLabel.frame)-5];
    }

}

@end
